package com.zoo.diannaopeijian.controller;

import com.github.pagehelper.PageInfo;
import com.zoo.diannaopeijian.domain.Staff;
import com.zoo.diannaopeijian.service.StaffService;
import com.zoo.diannaopeijian.util.MD5Util;
import groovy.lang.GrabExclude;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zx583 on 2018-01-08.
 */
@Controller
@RequestMapping(value = "/staff*")
public class StaffController extends BaseController {
    @Resource
    private StaffService staffService;

    @GetMapping(value = "staffManaPage")
    public String StaffManaPageRoute() {
        return "staff/staffManaPage";
    }

    @GetMapping(value = "staffAddPage")
    public String StaffAddPageRoute() {
        return "staff/staffAddPage";
    }

    @GetMapping(value = "staffUpdatePage")
    public String StaffUpdatePageRoute() {
        return "staff/staffUpdatePage";
    }

    @PostMapping(value = "staffGet")
    @ResponseBody
    public Map<String, Object> StaffGet(@RequestParam(required = true, defaultValue = "1") Integer page,
                                        @RequestParam(required = true, defaultValue = "10") Integer rows,
                                        @RequestParam(required = true, defaultValue = "false") boolean isFuzzy,
                                        Staff staff
    ) {
        Map<String, Object> result = new HashMap<>();

        List<Staff> datas = isFuzzy ? staffService.getFuzzyStaffWithPage(page, rows, staff) : staffService.getStaffWithPage(page, rows, staff);

        result.put(rowsWord, datas);
        result.put(totalWord, new PageInfo<Staff>(datas).getTotal());

        return result;
    }

    @PostMapping(value = "staffPost")
    public @ResponseBody
    String StaffPost(Staff staff) {
        return staffService.addStaff(staff) ? success : fail;
    }

    @PostMapping(value = "staffDelete")
    @ResponseBody
    public String StaffDelete(Integer staffid) {
        return staffService.deleteStaff(staffid) ? success : fail;
    }

    @PostMapping(value = "staffUpdate")
    @ResponseBody
    public String StaffUpdate(Staff staff) {
        if (staff.getStaffpassword() != null) {
            staff.setStaffpassword(MD5Util.encode(staff.getStaffpassword(), staffService.getStaffSalt(staff.getStaffid())));
        }
        return staffService.updateStaff(staff) ? success : fail;
    }
}
